<script setup>
import { reactive, ref, onMounted } from 'vue';
import { page, deleteById ,save} from "@/api/postInfo";
import { useRouter } from "vue-router";
const loading = ref(false)
const router = useRouter();
const queryPageParam = reactive({
  name: '',
  pageNo: 1,
  pageSize: 5
});

// 数据
const tableData = ref([]);
const total = ref(0);

const handleQueryPage = () => {
  page(queryPageParam).then(res => {
    console.log(res);
    tableData.value = res.data.records;
    total.value = res.data.total; // 把查询的总条数赋值给 total 变量
  });
};

// 修改每页数量的回调函数
const handleSizeChange = (sizeValue) => {
  queryPageParam.pageSize = sizeValue;
  handleQueryPage();
};

// 修改页码之后的回调函数
const handleCurrentChange = (currentValue) => {
  queryPageParam.pageNo = currentValue;
  handleQueryPage();
};

// 查看
const handleDetail = (id) => {
  router.push({
    path: '/postInfo/detail',
    query: { id }
  });
};

// 编辑
const handleEdit = (id) => {
  router.push({
    path: '/postInfo/save',
    query: { id }
  });
};

// 确认删除
const handleDeleteConfirm = (id) => {
  deleteById(id).then(() => {
    handleQueryPage(); // 删除成功后再次分页查询
  });
};

// 新增
const handleAdd = () => {
  router.push({
    path: '/postInfo/save'
  });
};

// Vue 挂载完成之后执行钩子函数
onMounted(() => {
  handleQueryPage();
});
const handleStatusChange = async (row) => {
  try {
    if(row.status===0){
      row.status===1;
    }else{
      row.status===0;
    }
    console.log("成功")
    await save(row); // 调用 API
  } catch (error) {
    console.log("失败")
    // 失败时恢复原来的状态
    row.status = row.status === 1 ? 0 : 1;
  }
};
</script>

<template>
  <div class="position-management-container">
    <!-- 头部区域 -->
    <div class="header-section">
      <h2 class="header-title">
        <el-icon class="title-icon"><User /></el-icon>
        岗位管理系统
      </h2>
      <div class="action-buttons">
        <el-input
          v-model="queryPageParam.name"
          placeholder="请输入岗位名称"
          class="search-input"
          clearable
          @clear="handleQueryPage"
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button type="primary" class="query-button" @click="handleQueryPage">
          <el-icon><Search /></el-icon>查询
        </el-button>
        <el-button type="success" class="add-button" @click="handleAdd">
          <el-icon><Plus /></el-icon>新增岗位
        </el-button>
      </div>
    </div>

    <!-- 数据表格 -->
    <el-card class="data-card" shadow="never">
      <el-table
        :data="tableData"
        style="width: 100%"
        stripe
        highlight-current-row
        class="elegant-table"
        v-loading="loading"
      >
        <el-table-column prop="positionId" label="岗位编号" width="120" align="center" fixed />
        <el-table-column prop="name" label="岗位名称" width="180" />
        
        <el-table-column prop="baseSalary" label="基础薪资" width="150" align="center">
          <template #default="scope">
            <span class="salary-text">{{ scope.row.baseSalary }} 元</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="status" label="岗位状态" width="150" align="center">
          <template #default="scope">
            <div class="status-container">
              <el-switch
                v-model="scope.row.status"
                :active-value="1"
                :inactive-value="0"
                active-color="#67C23A"
                inactive-color="#F56C6C"
                @change="handleStatusChange(scope.row)"
              />
              <span class="status-text" :class="{'active': scope.row.status === 1}">
                {{ scope.row.status === 1 ? '启用' : '禁用' }}
              </span>
            </div>
          </template>
        </el-table-column>
        
        <el-table-column prop="createTime" label="创建时间" width="180" align="center" />
        <el-table-column prop="updateTime" label="更新时间" width="180" align="center" />
        
        <el-table-column label="操作" width="250" fixed="right" align="center">
          <template #default="scope">
            <div class="action-buttons">
              <el-button
                size="small"
                class="view-button"
                @click="handleDetail(scope.row.positionId)"
              >
                <el-icon><View /></el-icon>查看
              </el-button>
              <el-button
                type="primary"
                size="small"
                class="edit-button"
                @click="handleEdit(scope.row.positionId)"
              >
                <el-icon><Edit /></el-icon>编辑
              </el-button>
              <el-popconfirm
                title="确定要删除这个岗位吗？"
                @confirm="handleDeleteConfirm(scope.row.positionId)"
              >
                <template #reference>
                  <el-button
                    type="danger"
                    size="small"
                    class="delete-button"
                  >
                    <el-icon><Delete /></el-icon>删除
                  </el-button>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="queryPageParam.pageNo"
        v-model:page-size="queryPageParam.pageSize"
        :page-sizes="[5, 10, 20, 30]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<style scoped>
.position-management-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 40px);
}

/* 头部区域 */
.header-section {
  margin-bottom: 20px;
  padding: 20px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.header-title {
  display: flex;
  align-items: center;
  font-size: 22px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.title-icon {
  margin-right: 12px;
  color: #4a6bdf;
  font-size: 28px;
}

.action-buttons {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}

.search-input {
  width: 280px;
}

.search-input :deep(.el-input__inner) {
  height: 40px;
  border-radius: 8px;
  padding-left: 36px;
}

.query-button,
.add-button {
  height: 40px;
  padding: 0 20px;
  border-radius: 8px;
  font-weight: 500;
}

.add-button {
  background: linear-gradient(120deg, #67c23a 0%, #5daf34 100%);
  border: none;
}

/* 数据卡片 */
.data-card {
  border-radius: 12px;
  margin-bottom: 20px;
  border: none;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

.elegant-table {
  --el-table-border-color: transparent;
  --el-table-header-bg-color: #f5f8ff;
}

.elegant-table :deep(th) {
  background-color: #f5f8ff;
  color: #4a6bdf;
  font-weight: 600;
  height: 56px;
}

.elegant-table :deep(td) {
  height: 60px;
  border-bottom: 1px solid #f0f4f8;
}

/* 状态样式 */
.status-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.status-text {
  font-weight: 500;
}

.status-text.active {
  color: #67C23A;
}

/* 薪资文本 */
.salary-text {
  font-weight: 600;
  color: #E6A23C;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  gap: 8px;
}

.view-button,
.edit-button,
.delete-button {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 6px;
  font-weight: 500;
}

.view-button {
  background: #e3f2fd;
  color: #2196f3;
}

.edit-button {
  background: #e8f5e9;
  color: #4caf50;
}

.delete-button {
  background: #ffebee;
  color: #f44336;
}

/* 分页 */
.pagination-container {
  display: flex;
  justify-content: center;
  padding: 16px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-section {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  
  .action-buttons {
    width: 100%;
    flex-wrap: wrap;
  }
  
  .search-input {
    width: 100%;
  }
  
  .action-buttons {
    flex-direction: column;
    gap: 8px;
  }
  
  .action-buttons .el-button {
    width: 100%;
  }
}
</style>